package com.yihusitian.zsxq.sg.util;

import cn.hutool.core.io.FileUtil;
import com.google.common.collect.Maps;
import com.yihusitian.zsxq.sg.config.*;
import lombok.Getter;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * @Description 配置信息解析类
 * @Author LeeHo
 * @Date 2020/8/26 13:48
 */
public class GenerateConfigHolder {

    //配置
    @Getter
    private GenerateConfig generateConfig;

    //基本配置信息
    @Getter
    private BaseInfo baseinfo;

    //数据源配置信息
    @Getter
    private DatasourceInfo datasource;

    //表配置信息
    @Getter
    private TablesInfo tables;

    //持久化对象
    @Getter
    private DOInfo doInfo;

    //VO对象
    @Getter
    private VOInfo voInfo;

    //数据传输对象
    @Getter
    private DTOInfo dtoInfo;

    //Mapper
    @Getter
    private MapperInfo mapperInfo;

    //Service接口
    @Getter
    private ServiceInfo serviceInfo;

    //Service实现
    @Getter
    private ServiceImplInfo serviceImplInfo;

    //所有的表信息
    @Getter
    private List<TableInfo> tableInfoList;

    //代码模板信息
    @Getter
    private Map<String, String> codeTemplateMap;

    private String configXmlFile = "generate-code/generate-config.xml";

    private String codeTemplateDir = "generate-code/template";

    public GenerateConfigHolder() {
        doInit();
    }

    /**
     * 执行初始化
     */
    private void doInit() {
        generateConfig = parseGenerateConfig();
        Assert.notNull(generateConfig, "配置信息解析为空");
        baseinfo = generateConfig.getBaseinfo();
        Assert.notNull(baseinfo, "基本配置信息为空");
        datasource = generateConfig.getDatasource();
        Assert.notNull(datasource, "数据源配置信息为空");
        tables = generateConfig.getTables();
        Assert.notNull(tables, "表配置信息为空");
        tableInfoList = tables.getTables();
        Assert.notEmpty(tableInfoList, "表名列表不可为空");
        codeTemplateMap = loadCodeTempalte();
        Assert.notEmpty(codeTemplateMap, "代码模板信息不可为空");
        doInfo = generateConfig.getDoInfo();
        Assert.notNull(doInfo, "持久化对象配置信息为空");
        voInfo = generateConfig.getVoInfo();
        Assert.notNull(voInfo, "VO对象配置信息为空");
        dtoInfo = generateConfig.getDtoInfo();
        Assert.notNull(dtoInfo, "DTO对象配置信息为空");
        mapperInfo = generateConfig.getMapperInfo();
        Assert.notNull(mapperInfo, "Mapper对象配置信息为空");
        serviceInfo = generateConfig.getServiceInfo();
        Assert.notNull(serviceInfo, "Service对象配置信息为空");
        serviceImplInfo = generateConfig.getServiceImplInfo();
        Assert.notNull(serviceImplInfo, "ServiceImpl对象配置信息为空");
    }

    /**
     * 加载代码模板信息
     *
     * @return
     */
    private Map<String, String> loadCodeTempalte() {
        Map<String, String> codeTemplateMap = Maps.newHashMap();
        FileUtil.loopFiles(codeTemplateDir).forEach(file -> {
            codeTemplateMap.put(file.getName(), FileUtil.readUtf8String(file));
        });
        return codeTemplateMap;
    }

    /**
     * 解析配置信息
     *
     * @return
     */
    private GenerateConfig parseGenerateConfig() {
        try {
            String xml = FileUtil.readUtf8String(configXmlFile);
            return JAXBUtil.fromXML(GenerateConfig.class, xml);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}